//
//  BPUIScrollSegment.m
//  AFNetworking
//
//  Created by 404 on 2020/7/31.
//

#import "BPUIScrollSegment.h"
#import "BPUICore.h"

@interface BPUIScrollSegment ()
@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) UIView *lineView;
@property(nonatomic, strong) NSMutableArray *buttons;
@property(nonatomic, assign) CGFloat itemWidth;
@end

@implementation BPUIScrollSegment

- (instancetype)init{
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setTypes:(NSArray *)types{
    if (!types || _types == types) {
        return;
    }
    _types = types;
    [self updateView];
}


- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:scrollView];
    scrollView.bounces = NO;
    self.scrollView = scrollView;
    
    [self updateView];
}

- (void)updateView {
    
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.buttons removeAllObjects];
    if (self.types.count == 0) {
        return;
    }
    float kspace = 10;
    self.itemWidth = (SCREEN_WIDTH - kspace * 4 - 15 * 2)/5.0;
    
    float width = self.types.count * self.itemWidth + 15 * 2 + (self.types.count - 1) * kspace;
    width = width < SCREEN_WIDTH ? SCREEN_WIDTH : width;
    self.scrollView.contentSize = CGSizeMake(width, 50);
    
    NSArray *types = [NSArray arrayWithArray:self.types];
    for (int i = 0; i < types.count; i++) {
        UIButton *btn = [self typeButtonWithTitle:types[i] tag:i];
        [self.scrollView addSubview:btn];
        CGFloat x = 15 + kspace * i + self.itemWidth * i;
        btn.frame = CGRectMake(x, btn.frame.origin.y, btn.frame.size.width, btn.frame.size.height);
        [self.buttons addObject:btn];
    }
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(15, 60 - 2 - 4, self.itemWidth, 4)];
    self.lineView.layer.cornerRadius = 2;
    self.lineView.backgroundColor = UIColorMake(0, 149, 252);
    [self.scrollView addSubview:self.lineView];
}

- (UIButton*)typeButtonWithTitle:(NSString*)title tag:(NSInteger)tag{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 10, self.itemWidth, 40)];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.tag = tag;
    button.titleLabel.font = UIFontMake(16);
    return button;
}

- (void)buttonClick:(UIButton*)btn {
    if (self.BPUIScrollSegmentViewBtnClickHandle) {
        self.BPUIScrollSegmentViewBtnClickHandle(self, btn.tag);
    }
}
- (void)segmentScrollToIndex:(NSInteger)index animated:(BOOL)animated{
    if (self.buttons.count == 0) {
        return;
    }
    UIButton *btn = self.buttons[index];
    CGFloat off_x = btn.left - 15 -10 * 2 - self.itemWidth * 2;
    CGFloat minX = 0;
    CGFloat maxX = self.scrollView.contentSize.width - SCREEN_WIDTH;
    if (maxX < 0) {
        maxX = 0;
    }
    if (off_x < minX) {
        off_x = minX;
    }else if (off_x > maxX){
        off_x = maxX;
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.lineView.frame = CGRectMake(btn.left,CGRectGetMinY(self.lineView.frame), CGRectGetWidth(self.lineView.frame), CGRectGetHeight(self.lineView.frame));
        [self.scrollView setContentOffset:CGPointMake(off_x, 0) animated:NO];
    }];
}

- (NSMutableArray *)buttons{
    if (!_buttons) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

@end
